<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      size="small"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.name"
          placeholder="分组名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    分组图标还没有
    <el-table
      :data="dataList"
      border
      size="mini"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      ></el-table-column>
      <el-table-column type="index" align="center" label="编号" width="80" />
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="分组名称"
      ></el-table-column>
      <el-table-column
        prop="icon"
        header-align="center"
        align="center"
        label="分组图标"
      ></el-table-column>
      <el-table-column
        prop="orderNum"
        header-align="center"
        align="center"
        label="排序"
      ></el-table-column>

      <el-table-column
        prop="dataState"
        header-align="center"
        align="center"
        label="显示状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.dataState === -1" size="small" type="danger"
            >禁用</el-tag
          >
          <el-tag v-else size="small">启用</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="200"
        label="操作"
      >
        <template slot-scope="scope">
          <el-dropdown @command="handleCommand(scope.row, $event)">
            <el-button size="mini">
              绑定 <i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="updateAttr">绑定规格属性</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

          <el-dropdown @command="handleCommand(scope.row, $event)">
            <el-button size="mini">
              操作 <i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="addOrUpdateHandle"
                >修改</el-dropdown-item
              >
              <el-dropdown-item command="deleteHandle">删除</el-dropdown-item>
              <el-dropdown-item
                v-if="scope.row.dataState === -1"
                command="enableHandle"
                >启用</el-dropdown-item
              >
              <el-dropdown-item v-else command="disableHandle"
                >冻结</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>

    <add-or-update-attr
      v-if="addOrUpdateAttrVisible"
      ref="addOrUpdateAttr"
      @refreshDataList="getDataList"
    ></add-or-update-attr>

  </div>
</template>

<script>
import AddOrUpdate from "./attrgroup-add-or-update";
import AddOrUpdateAttr from "./attrgroup-add-or-update-attr";
import {
  productAttrGroupPageList,
  removeProductAttrGroup,
  enableProductAttrGroup,
  disableProductAttrGroup,
} from "@/api/product";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: { AddOrUpdate, AddOrUpdateAttr },
  props: {},
  data() {
    return {
      dataForm: {
        name: "",
        catId: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      addOrUpdateAttrVisible: false,
      addOrUpdateCategoryVisible: false,
    };
  },
  activated() {
    this.getDataList();
  },
  methods: {
    handleCommand(row, command) {
      if (command == "enableHandle") {
        this.enableHandle(row);
      } else if (command == "disableHandle") {
        this.disableHandle(row);
      } else if (command == "deleteHandle") {
        this.deleteHandle(row.id);
      } else if (command == "addOrUpdateHandle") {
        this.addOrUpdateHandle(row, 2);
      } else if (command == "updateAttr") {
        this.updateAttr(row);
      }
    },
    //处理分组与属性的关联
    relationHandle(id) {
      this.relationVisible = true;
      this.$nextTick(() => {
        this.$refs.relationUpdate.init(id);
      });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      productAttrGroupPageList({
        pageNumber: this.pageIndex,
        pageSize: this.pageSize,
        categoryId: this.dataForm.catId,
        name: this.dataForm.name,
      }).then((res) => {
        if (res && res.code === 0) {
          this.dataList = res.data.data;
          this.totalPage = res.data.count;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(data) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data);
      });
    },
    updateAttr(data) {
      this.addOrUpdateAttrVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdateAttr.init(data);
      });
    },
    enableHandle(row) {
      this.$confirm(`确定进行启用操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          enableProductAttrGroup({ id: row.id }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    disableHandle(row) {
      this.$confirm(`确定进行冻结操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          disableProductAttrGroup({ id: row.id }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(`确定是否进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        removeProductAttrGroup({ ids: ids }).then((res) => {
          if (res && res.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(res.msg);
          }
        });
      });
    },
  },
};
</script>
<style scoped>
</style>